package com.yang.qgg_demo.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.qgg_demo.common.R;
import com.yang.qgg_demo.entity.Bill;
import com.yang.qgg_demo.entity.Goods;
import com.yang.qgg_demo.entity.User;
import com.yang.qgg_demo.mapper.GoodsMapper;
import com.yang.qgg_demo.service.BillService;
import com.yang.qgg_demo.service.GoodsService;
import com.yang.qgg_demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("echarts")
public class EchartsController {

    @Autowired
    private BillService billService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private IUserService userService;


    @GetMapping("/line")
    public Map<String, Object> getEchartsDataline() {
        Map<String, Object> response = new HashMap<>();

        // 获取按日期汇总的总金额
        List<Map<String, Object>> totalMoneyByDate = billService.getTotalMoneyByDate();

        // 将日期和总金额存入 response
        for (Map<String, Object> entry : totalMoneyByDate) {
            String date = entry.get("date").toString();
            String money = entry.get("total_money").toString();
            response.put(date, money);
        }

        return response;
    }


    @GetMapping("/pie")
    public Map<String, Object> getEchartsDatalpie() {
        Map<String, Object> response = new HashMap<>();
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        List<Goods> goodsList = goodsMapper.selectList(queryWrapper);
        for (Goods goods : goodsList) {
            String name = goods.getName();
            Integer number = goods.getNumber();
            response.put(name, number);
        }
        return response;
    }

    @GetMapping("/data")
    public Map<String, Object> getEchartsDatal() {
        Map<String, Object> response = new HashMap<>();
        response.put("userNumber",userService.findAll());
        response.put("addmoney",billService.addBill());
        response.put("profit",billService.addprofit());
        response.put("goodsnumber",goodsService.addGoods());
        return response;
    }

}
